import time

from selenium import webdriver
import time

from selenium.webdriver.common.by import By

    #实例化类,进入163邮箱地址
class Email:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://email.163.com/')
        self.driver.maximize_window()

    # 登录邮箱
    def login(self):
        f1=self.driver.find_element(By.XPATH,"//iframe[contains(@id,'x-URS-iframe')]" )
        self.driver.switch_to.frame(f1)
        self.driver.find_element(By.XPATH,"//input[@data-placeholder='邮箱帐号或手机号码']" ).send_keys('sun2219915383')
        self.driver.find_element(By.XPATH,"//input[@data-placeholder='输入密码']").send_keys('556123sun.')
        self.driver.find_element(By.ID,'dologin').click()

    #定位写信,点击
    def write(self):
        time.sleep(3)
        # 定位写信,点击
        self.driver.find_element(By.ID, "_mail_component_149_149").click()
        #定位收件人.写入
        self.driver.find_element(By.XPATH,"//input [@class='nui-editableAddr-ipt']").send_keys('srx0921@163.com')
        time.sleep(3)
        #定位主题,写入
        self.driver.find_element(By.CSS_SELECTOR,'input[id*="_subjectInput"]').send_keys('你好')
        #定位iframe切换
        frame1=self.driver.find_element(By.CLASS_NAME,'APP-editor-iframe')
        self.driver.switch_to.frame(frame1)
        #正文定位,写入
        self.driver.find_element(By.CLASS_NAME,"nui-scroll").send_keys('我是xxx')
        #退出主页面,定位当前和点击按钮
        self.driver.switch_to.default_content()
        self.driver.find_element(By.ID,"//*[@class='frame-main-cont-head']/div/div/div/span[2]").click()
        print('发送成功')


    def quit(self):
        time.sleep(5)
        self.driver.quit()


if __name__ == '__main__':
    l1 = Email()
    l1.login()
    l1.write()
    l1.quit()